﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.MemoryMappedFiles;

namespace InCom.Core.TerminalSend.MappedFile
{
    /// <summary>
    /// 数据实体定义 
    /// </summary>
    public class MappedDataEntity
    {
        private MDateTime mDate = null;
        /// <summary>
        /// 时间
        /// </summary>
        public DateTime Date
        {
            get
            {
                if (mDate == null)
                {
                    mDate = new MDateTime(GetOffset(8), Access);
                }
                return mDate.Value;
            }
            set
            {
                if (mDate == null)
                {
                    mDate = new MDateTime(GetOffset(8), Access);
                }
                mDate.Value = value;
            }

        }


        private MInt testint = null;
        /// <summary>
        /// int值
        /// </summary>
        public int Testint
        {
            get
            {
                if (testint == null)
                {
                    testint = new MInt(GetOffset(4), Access);
                }
                return testint.Value;
            }
            set
            {
                if (testint == null)
                {
                    testint = new MInt(GetOffset(4), Access);
                }
                testint.Value = value;
            }
        }

        private MString _Name = null;
        /// <summary>
        /// 名称最大长度支持1024个字符
        /// </summary>
        public string Name
        {
            get
            {
                if (_Name == null)
                {
                    _Name = new MString(GetOffset(1024 * 3 + 8), Access, 1024);
                }
                return _Name.Value;
            }
            set
            {
                if (_Name == null)
                {
                    _Name = new MString(GetOffset(1024 * 3 + 8), Access, 1024);
                }
                _Name.Value = value;
            }
        }
        private MString _Name1 = null;
        /// <summary>
        /// 名称最大长度支持1024个字符
        /// </summary>
        public string Name1
        {
            get
            {
                if (_Name1 == null)
                {
                    _Name1 = new MString(GetOffset(_Name.FileMaxLength), Access, 1024);
                }
                return _Name1.Value;
            }
            set
            {
                if (_Name1 == null)
                {
                    _Name1 = new MString(GetOffset(_Name.FileMaxLength), Access, 1024);
                }
                _Name1.Value = value;
            }
        }

        /// <summary>
        /// 当前类的大小
        /// </summary>
        /// <param name="startoffset"></param>
        public MappedDataEntity(long startoffset, MemoryMappedViewAccessor access)
        {
            offset = StartOffset = startoffset;
            Access = access;

            init();
        }

        /// <summary>
        /// 初始化属性,这个类必须在创建时调用
        /// </summary>
        protected void init()
        {
            var d = Testint;
            var s = Name;
            var ds = Name1;
            //初始化日期时间
            var dt = Date;
        }
        /// <summary>
        /// 视图
        /// </summary>
        protected MemoryMappedViewAccessor Access { get; set; }

        /// <summary>
        /// 当前偏移量用于标识各个不同的类
        /// </summary>
        private long offset;
        /// <summary>
        /// 传入对像长度获取当前offset
        /// </summary>
        /// <param name="objLength">对像长度</param>
        /// <returns></returns>
        protected long GetOffset(int objLength)
        {
            long result = offset;
            offset += objLength;
            //结束标识
            EndOffset = offset;
            return result;
        }

        /// <summary>
        /// 当前类的大小
        /// </summary>
        public int Size { get { return (int)(EndOffset - StartOffset); } }
        /// <summary>
        /// 载始偏移量
        /// </summary>
        public long StartOffset { get; protected set; }
        /// <summary>
        /// 结束偏移量
        /// </summary>
        public long EndOffset { get; protected set; }
    }
}
